< previous page page_536 next page >

Page 536
Cache Frequently Used Properties in Variables
You can get and set values of variables faster than those of properties. If you're getting the value of a property frequently (such as in a loop), your code runs faster if you assign the property to a variable outside the loop and then use the variable instead of the property. Variables are generally 10 to 20 times faster than properties of the same type. The following code is very slow:
Do Until EOF(F)
   Line Input #F, nextLine
   Text1.Text = Text1.Text + nextLine
Loop
This version of the code runs much more quickly:
Do Until EOF(F)
   Line Input #F, nextLine
   bufferVar = bufferVar + nextLine
Loop
Text1.Text = bufferVar
Replace Procedure Calls with Inline Procedures
Although procedures help make your code more modular, every procedure call involves some additional work and time for your application. If you have a loop that calls a procedure several times, you can eliminate this overhead by placing the body of the procedure directly in the loop.
Use Constants Whenever Possible
Using constants makes your application run faster. Constants also make your code more readable and easier to maintain. If some strings or numbers in your code don't change, declare them as constants. Constants are resolved once at compile time. Because variables must be resolved each time the application runs and finds a variable, the application has to access the variable in memory to get the current value.
Pass Arguments ByVal Instead of ByRef
When writing procedures that include arguments, it's faster to pass the arguments by value (ByVal) than to pass them by reference (ByRef). If you don't need to modify the arguments within the procedure, you should define them as ByVal.
Compiling Versus Using P-Code
When you compile your Visual Basic application, you're actually creating a file that will be interpreted during execution by the Visual Basic dynamic link library (DLL). This type of compilation is called a P-code executable. The Professional and Enterprise Editions of Visual Basic include an option to compile your application to a native code. EXE.

 
< previous page page_536 next page >

If you like this book, buy it!